package com.gzu.live.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

public class UploadImgUtil {

    //阿里域名
    public static final String ALI_DOMAIN = "https://liangwenjun.oss-cn-hangzhou.aliyuncs.com/";
    //地域节点
    public static final String endpoint="http://oss-cn-hangzhou.aliyuncs.com";
    //访问ID
    public static final String accessKeyId="LTAI5tQtoajdvBUMBaxi2fGo";
    //访问密钥
    public static final String accessKeySecret="ovHcCj4nI3M8uIavlkOaAJv94HVquQ";


    //生成文件名
    public static String uploadImage(MultipartFile file){
        //生成文件名
        String orginalFilename=file.getOriginalFilename();
        String ext= "."+ FilenameUtils.getExtension(orginalFilename);
        String uuid=UUID.randomUUID().toString().replace("-","");
        String newFileName=uuid+ext;

        //OSS客户端对象
        OSS ossClient=new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);

        //上传图片(仓库名)
        try {
            ossClient.putObject("liangwenjun",newFileName,file.getInputStream());
        } catch (IOException e) {
            return null;
        }finally {
            ossClient.shutdown();
        }
        return ALI_DOMAIN + newFileName;
    }
}
